/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package MeteoCal.controller;

import MeteoCal.bean.exceptions.NonexistentEntityException;
import MeteoCal.bean.exceptions.PreexistingEntityException;
import MeteoCal.bean.exceptions.RollbackFailureException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import MeteoCal.entity.Comment;
import MeteoCal.entity.Commentwritten;
import MeteoCal.entity.CommentwrittenPK;
import MeteoCal.entity.Event;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.transaction.UserTransaction;

/**
 *
 * @author Daniele
 */
public class CommentwrittenJpaController implements Serializable {

    public CommentwrittenJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Commentwritten commentwritten) throws PreexistingEntityException, RollbackFailureException, Exception {
        if (commentwritten.getCommentwrittenPK() == null) {
            commentwritten.setCommentwrittenPK(new CommentwrittenPK());
        }
        commentwritten.getCommentwrittenPK().setIdEvent(commentwritten.getEvent().getIdEvent());
        commentwritten.getCommentwrittenPK().setIdComment(commentwritten.getComment().getIdComment());
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Comment comment = commentwritten.getComment();
            if (comment != null) {
                comment = em.getReference(comment.getClass(), comment.getIdComment());
                commentwritten.setComment(comment);
            }
            Event event = commentwritten.getEvent();
            if (event != null) {
                event = em.getReference(event.getClass(), event.getIdEvent());
                commentwritten.setEvent(event);
            }
            em.persist(commentwritten);
            if (comment != null) {
                comment.getCommentwrittenCollection().add(commentwritten);
                comment = em.merge(comment);
            }
            if (event != null) {
                event.getCommentwrittenCollection().add(commentwritten);
                event = em.merge(event);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            if (findCommentwritten(commentwritten.getCommentwrittenPK()) != null) {
                throw new PreexistingEntityException("Commentwritten " + commentwritten + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Commentwritten commentwritten) throws NonexistentEntityException, RollbackFailureException, Exception {
        commentwritten.getCommentwrittenPK().setIdEvent(commentwritten.getEvent().getIdEvent());
        commentwritten.getCommentwrittenPK().setIdComment(commentwritten.getComment().getIdComment());
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Commentwritten persistentCommentwritten = em.find(Commentwritten.class, commentwritten.getCommentwrittenPK());
            Comment commentOld = persistentCommentwritten.getComment();
            Comment commentNew = commentwritten.getComment();
            Event eventOld = persistentCommentwritten.getEvent();
            Event eventNew = commentwritten.getEvent();
            if (commentNew != null) {
                commentNew = em.getReference(commentNew.getClass(), commentNew.getIdComment());
                commentwritten.setComment(commentNew);
            }
            if (eventNew != null) {
                eventNew = em.getReference(eventNew.getClass(), eventNew.getIdEvent());
                commentwritten.setEvent(eventNew);
            }
            commentwritten = em.merge(commentwritten);
            if (commentOld != null && !commentOld.equals(commentNew)) {
                commentOld.getCommentwrittenCollection().remove(commentwritten);
                commentOld = em.merge(commentOld);
            }
            if (commentNew != null && !commentNew.equals(commentOld)) {
                commentNew.getCommentwrittenCollection().add(commentwritten);
                commentNew = em.merge(commentNew);
            }
            if (eventOld != null && !eventOld.equals(eventNew)) {
                eventOld.getCommentwrittenCollection().remove(commentwritten);
                eventOld = em.merge(eventOld);
            }
            if (eventNew != null && !eventNew.equals(eventOld)) {
                eventNew.getCommentwrittenCollection().add(commentwritten);
                eventNew = em.merge(eventNew);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                CommentwrittenPK id = commentwritten.getCommentwrittenPK();
                if (findCommentwritten(id) == null) {
                    throw new NonexistentEntityException("The commentwritten with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(CommentwrittenPK id) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Commentwritten commentwritten;
            try {
                commentwritten = em.getReference(Commentwritten.class, id);
                commentwritten.getCommentwrittenPK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The commentwritten with id " + id + " no longer exists.", enfe);
            }
            Comment comment = commentwritten.getComment();
            if (comment != null) {
                comment.getCommentwrittenCollection().remove(commentwritten);
                comment = em.merge(comment);
            }
            Event event = commentwritten.getEvent();
            if (event != null) {
                event.getCommentwrittenCollection().remove(commentwritten);
                event = em.merge(event);
            }
            em.remove(commentwritten);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Commentwritten> findCommentwrittenEntities() {
        return findCommentwrittenEntities(true, -1, -1);
    }

    public List<Commentwritten> findCommentwrittenEntities(int maxResults, int firstResult) {
        return findCommentwrittenEntities(false, maxResults, firstResult);
    }

    private List<Commentwritten> findCommentwrittenEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Commentwritten.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Commentwritten findCommentwritten(CommentwrittenPK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Commentwritten.class, id);
        } finally {
            em.close();
        }
    }

    public int getCommentwrittenCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Commentwritten> rt = cq.from(Commentwritten.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
